
const catId = json.getUrlData("catId");
const keywords = json.getUrlData("keywords");
const catName = json.getUrlData("catName");
let list = document.querySelector(".product>ul");
let pagein = document.querySelector(".pagein");
// console.log(list);
(function(){
	
	console.log(keywords)
	if(catId != null){
		pageinfo();
		pagein.innerHTML = catName;
	}else{
		search()
		pagein.innerHTML = keywords;
	};
	
	
    getpage();
	

})();

//请求返回页数
function getpage(callback){
	
	if(catId != null){
		json.get(URL+`/api_goods`,{catId : catId},even =>{
			if(even.code != 0) return;
			  console.log(even)
			  callback(even.page);
			  // console.log(callback)
				 
		});
	}else{
		let pageNum = document.querySelector(".y");
		
		pageNum.style.display = "none";
		// json.get(URL + "/api_search",{
				
		// 		keywords :keywords,
		// 	},even =>{
		// 		console.log(parseInt(even.count/9))
		// 		 callback(parseInt(even.count/9));
				
		// 	});
	}
	
	
};

//页码控制
getpage(function(count){
	let pageNum = document.querySelector(".y");

	let pavepage = document.querySelector(".y :nth-child(1)");
	let maxPage = count;
	let next = document.querySelector(".y :nth-child(9)")
	let pageNumber = pageNum.querySelectorAll("a");
	let nowPage = 1;
	let querText = document.querySelector(".querText")
	let querBnt = document.querySelector(".querBnt")
	pageNumber[0].innerHTML = "1";
	pageNumber[1].innerHTML = "2";
	pageNumber[2].innerHTML = "3";
	pageNumber[3].innerHTML = "4";
	pageNumber[4].innerHTML = "...";
	pageNumber[5].innerHTML = maxPage-1;
	pageNumber[6].innerHTML = maxPage;
	
	//输入页码跳转
	querBnt.onclick = function(){
		console.log(querText.value);
		// pageinfo(querText.value);
		pageNUM(querText.value);
	} 
	
	
	pageNumber.forEach((item,index) =>{
			// console.log(index)
		item.onclick = function(){
			nowPage = this.innerHTML;
			
			pageNUM(nowPage);
			// pageNumber.forEach(item =>{
			// 	item.style.backgroundColor = "white"
			// });
			// pageNumber[(nowPage)%7].style.backgroundColor = "#95B1D1"
		
		}	
	})		
	
	
	
	
	next.onclick = function(){
		
		
	    nowPage = parseInt(nowPage);
		nowPage++;
		nowPage = nowPage >= maxPage ? maxPage : nowPage;
		pageNUM(nowPage)
				
		
		
		console.log(nowPage);
		// pageinfo(nowPage);
	};
	pavepage.onclick = function(){
		
		
	    nowPage = parseInt(nowPage);
		nowPage--;
		nowPage = nowPage <= 0 ? 1 : nowPage;
		pageNUM(nowPage)
				
		
		
		console.log(nowPage);
		// pageinfo(nowPage);
	};
	
	function pageNUM(nowPage){
		// console.log((26-1)%7)
		if(lock) return;
		// 上锁
		lock = true;
		
		
		if(nowPage == "...") return;
		
		if(nowPage <= 1){
			pavepage.style.display = "none"
		}else{
			pavepage.style.display = "block"
		}
		//排他
		pageNumber.forEach(item =>{
			item.style.backgroundColor = "white"
		})
		
					
		nowPage = parseInt(nowPage);
		console.log(nowPage)
		// if(nowPage == maxPage){
		// 	pageNumber[(nowPage)%7].style.backgroundColor = "#95B1D1"
		
		// 	return;
		// }
		if(nowPage <= 3){
			pageNumber[0].innerHTML = "1";
			pageNumber[1].innerHTML = "2";
			pageNumber[2].innerHTML = "3";
			pageNumber[3].innerHTML = "4";
			pageNumber[4].innerHTML = "...";
			pageNumber[5].innerHTML = maxPage-1;
			pageNumber[6].innerHTML = maxPage;
			pageNumber[nowPage-1].style.backgroundColor = "#95B1D1"
		}else if(nowPage > 3 && nowPage < maxPage-2){
			pageNumber[0].innerHTML = "1";
			pageNumber[1].innerHTML = "...";
			pageNumber[2].innerHTML = nowPage - 1;
			pageNumber[3].innerHTML = nowPage;
			pageNumber[4].innerHTML = nowPage + 1;
			pageNumber[5].innerHTML = "...";
			pageNumber[6].innerHTML = maxPage;
			pageNumber[3].style.backgroundColor = "#95B1D1"
		}else if(nowPage >= maxPage-2){
			pageNumber[0].innerHTML = "1";
			pageNumber[1].innerHTML = "2";
			pageNumber[2].innerHTML = "..."
			pageNumber[3].innerHTML = maxPage - 3;
			pageNumber[4].innerHTML = maxPage - 2;
			pageNumber[5].innerHTML = maxPage - 1;
			pageNumber[6].innerHTML = maxPage;
			
			if(maxPage % 7 == 0){
				pageNumber[(nowPage-1)%7].style.backgroundColor = "#95B1D1"
			}else{
				pageNumber[(nowPage)%7].style.backgroundColor = "#95B1D1"
			}
		}
		
		console.log(catId)
		if(catId != null){
			pageinfo(nowPage);
		}else{
			console.log("111")
		  search(nowPage)
		}
	
		
		// pageinfo(nowPage);
	}
});	



//分类渲染请求
function pageinfo(page = 1){
	json.get(URL+`/api_goods`,{page : page , pagesize : 9 , catId : catId},even =>{
		console.log(even)
		product(even)
	});
}

//搜索渲染请求
function search(page = 1){
	const pagesize = json.getUrlData("pagesize");
	json.get(URL + "/api_search",{
		page : page,
		pagesize : pagesize,
		keywords :keywords,
	},even =>{
		console.log(even)
		
		product(even)
	});
}
//我拎出来
function product(even){
	if(even.code != 0 ) return;
	let str = "";
	even.data.forEach(item =>{
		// console.log(item);
		str += `
			<li>
				<div class="product-page">
					<img src="${item.goods_thumb}">
					<div class="maker">
						<a href="product.html?goodsId=${item.goods_id}">
							<p>${item.price}￥</p>
							<p>${item.goods_name}</p>
							<p>${item.goods_desc}</p>
						</a>
					</div>
				</div>
				<div class="product-info flex-row-col-center">
					<a class="flex-row align-center">
						<img src="${item.brand_thumb}">
						<span>${item.brand_name}</span>
					</a>
					<div>
						<span>${item.star_number}</span>
						<span>♥</span>
					</div>
				</div>
			</li>
		`;
	})
	lock = false;
	list.innerHTML = str;
}